package com.woniu.account.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.account.dto.GymAccountDto;
import com.woniu.account.dto.GymAccountRegisTryDto;
import com.woniu.account.form.GymCodeLoginForm;
import com.woniu.account.form.LoginGymAccountForm;
import com.woniu.account.form.RegistryGymAccountForm;
import com.woniu.account.model.TCoach;
import com.woniu.account.service.TGymAccountService;
import com.woniu.core.util.JwtUtil;
import com.woniu.core.util.RedisUtil;
import com.woniu.core.util.Result;
import com.woniu.core.util.ValidateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@RestController
@RequestMapping("/tGymAccount")
@Api(tags = "场馆账号表相关接口")
public class TGymAccountController {
    @Resource
    private TGymAccountService tGymAccountService;
    @Resource
    private RedisUtil redisUtil;
    @GetMapping("/findGymAccountById/{gymId}.gym")
    @ApiOperation("根据gymId查询场馆账号信息")
    public Result<GymAccountDto> findGymAccountById(@PathVariable("gymId") Integer gymId){
        GymAccountDto dto = tGymAccountService.findGymAccountById(gymId);
        return Result.getResult(GymAccountDto.class).setMsg("查询成功").setData(dto);
    }

    @PostMapping("/sendRegisterCode")
    @ApiOperation("发送注册短信验证码")
    public Result sendRegisterCode(String phone) throws Exception{
       tGymAccountService.sendRegisterCode(phone);
       return Result.getResult(Boolean.class).setMsg("发送验证码成功").setData(true);
    }

    @ApiOperation("场馆根据手机验证码注册账号")
    @PostMapping("/gymRegister.gym")
    public Result<Map<String, Object>> gymRegister(@RequestBody @Valid RegistryGymAccountForm form, BindingResult br) throws Exception{
        if (br.hasErrors()) return ValidateUtil.getValidateError(br);
        Boolean b= tGymAccountService.gymRegister(form);
        if (b) return Result.getMapResult(String.class, Object.class).setMsg("注册成功");
        return Result.getMapResult(String.class, Object.class).setMsg("注册失败");
    }

    @PostMapping("/sendLoginCode")
    @ApiOperation("发送登录短信验证码")
    public Result sendLoginCode(String phone) throws Exception{

        String code = tGymAccountService.sendLoginCode(phone);
        return Result.getResult(String.class).setMsg("发送验证码成功").setData(code);
    }

    //验证码登录
    @PostMapping("/codelogin")
    @ApiOperation("短信验证码登录")
    public Result<GymAccountRegisTryDto> codeLogin(@RequestBody GymCodeLoginForm form, HttpServletResponse response)throws Exception{
        GymAccountRegisTryDto dto=tGymAccountService.codeLogin(form);
        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("gymId",dto.getGymId());
        String refreshToken = JwtUtil.createToken(map,24*60,4);//有效期24小时
        String accessToken = JwtUtil.createToken(map, 30,4);
        redisUtil.set(accessToken,refreshToken,24L*60L*60L);
        response.setHeader("token",accessToken);
        return Result.getResult(GymAccountRegisTryDto.class).setMsg("登录成功").setData(dto);
    }

    @PostMapping("/gymAccountLogin")
    @ApiOperation("账号密码登录")
    public Result<GymAccountRegisTryDto> login(@RequestBody LoginGymAccountForm form, HttpServletResponse response) throws Exception {
        GymAccountRegisTryDto dto = tGymAccountService.login(form);
        //登录成功
        //生成JWT令牌
        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("gymId",dto.getGymId());
        String refreshToken = JwtUtil.createToken(map,24*60,4);//有效期24小时
        String accessToken = JwtUtil.createToken(map, 30,4);
        redisUtil.set(accessToken,refreshToken,24L*60L*60L);
        response.setHeader("token",accessToken);
        return Result.getResult(GymAccountRegisTryDto.class).setMsg("登录成功").setData(dto);
    }
}

